/**
 * @Title: ItemServiceImpl
 * @Package cn.com.yiqi.wsc.service.item.impl
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/2/21 17:36
 * @version V1.0
 */
package cn.com.yiqi.wsc.service.item.impl;

import cn.com.yiqi.wsc.Ro.ItemDetailVO;
import cn.com.yiqi.wsc.Ro.ItemInfoVO;
import cn.com.yiqi.wsc.common.BaseQueryDO;
import cn.com.yiqi.wsc.dao.shop.ItemMapper;
import cn.com.yiqi.wsc.entity.shop.Item;
import cn.com.yiqi.wsc.ro.ItemInfoSearchDto;
import cn.com.yiqi.wsc.ro.SubjectItemListQueryDto;
import cn.com.yiqi.wsc.service.file.dataobject.QiniuConfig;
import cn.com.yiqi.wsc.service.item.ItemService;
import cn.com.yiqi.wsc.service.item.enumerate.ItemKindEnum;
import cn.com.yiqi.wsc.service.item.enumerate.SearchAdType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 *@ClassName: ItemServiceImpl
 * Description: TODO
 * @author 会员系统组-林威
 * @date 2017/2/21 17:36
 *
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    ItemMapper itemDao;

    @Autowired
    QiniuConfig qiniuConfig;

    @Override
    public ItemDetailVO findItemDetail(Integer id,Integer userId) {
        return itemDao.findItemDetailByItemId(qiniuConfig.getURL()+"/",id,userId);
    }

    @Override
    public PageInfo<ItemInfoVO> findItemListBySubjectId(SubjectItemListQueryDto dto) {
        PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
        return new PageInfo<>(itemDao.findItemList(qiniuConfig.getURL()+"/",dto.getId()));
    }

    @Override
    public Integer countBySubjectType(Integer id) {
        return itemDao.countBySubject(id);
    }

    @Override
    public PageInfo<ItemInfoVO> finditemInfoByKey(ItemInfoSearchDto dto) {
        PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
        String str=null;
        if(null == dto.getOrder()){
            str = "c.d_insert_time desc";
        }else if(dto.getOrder() == 1){
            str = "c.n_sold_count desc";
        }else if(dto.getOrder() == 2){
            str = "c.n_sale_price asc";
        }else {
            str = "c.n_sale_price desc";
        }
        return new PageInfo<>(itemDao.selectItemIfoByKey(qiniuConfig.getURL()+"/",dto.getKey(),str,dto.getType()));
    }

    @Override
    public Integer countitemInfoByKey(ItemInfoSearchDto dto) {
        return itemDao.countItemIfoByKey(dto.getKey());
    }

    @Override
    public Integer countAdItem() {
       return itemDao.countAdItem();
    }

    @Override
    public PageInfo<ItemInfoVO> findAdItem(BaseQueryDO baseDo) {
        PageHelper.startPage(baseDo.getCurrentPage(), baseDo.getPageSize());
        return new PageInfo<>(itemDao.findAdItem(qiniuConfig.getURL()+"/",SearchAdType.AD.getValue()));
    }

    @Override
    public Item findByid(Integer itemId) {
        return itemDao.selectByPrimaryKey(itemId);
    }

    @Override
    public Item findItemByVipOrder(Integer orderId) {
        return itemDao.findItemByOrderId(orderId);
    }

    /**
     * 搜索热门商品
     * */
    public List<ItemInfoVO> findHotItem(){
      return itemDao.findAdItem(qiniuConfig.getURL()+"/",SearchAdType.HOTITEM.getValue());
    }

    /**
     * 搜索搜藏的商品
     * */
    public PageInfo<ItemInfoVO> selectLikeItemInfo(Integer userId, Integer pageSize, Integer currentPage) {
        PageHelper.startPage(currentPage,pageSize);

        return new PageInfo<>(itemDao.selectLikeItemInfo(qiniuConfig.getURL()+"/",userId));
    }

    /**
     * 搜索商品
     * */
    public ItemInfoVO findItemById(Integer itemId) {
        return itemDao.findItemInfo(qiniuConfig.getURL()+"/",itemId,null, ItemKindEnum.item.getValue()).get(0);
    }

    /**
     * 所有商品或者服务
     * */
    public PageInfo<ItemInfoVO> finditemInfo(Integer type,Integer pageSize, Integer currentPage,Integer kind) {
        PageHelper.startPage(currentPage,pageSize);
        return new PageInfo<>(itemDao.findItemInfo(qiniuConfig.getURL()+"/",null,type,kind));
    }

    /**
     * 积分兑换商品
     * */
    public List<ItemInfoVO> findPointItems() {
        return itemDao.findPointItem(qiniuConfig.getURL()+"/",null);
    }

    public List<ItemInfoVO> findVipItems() {
        List<ItemInfoVO> infoVOs = itemDao.findItemInfo(qiniuConfig.getURL()+"/",null,null,ItemKindEnum.vip.getValue());
        for(ItemInfoVO vo :infoVOs){
            vo.init();
        }

        return infoVOs;
    }
}
